﻿using System;
using System.Text;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;

using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Library;
using VIT.Library.Web;
using VIT.DataHelper.DataAccess;

namespace VIT.Web.Admin
{
    public partial class MatrixColorStyle : VITComponent
    {

        Product_Color_Style[] Matrix;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
                LoadData();
        }

        #region Methods
        private string check(int ColorId, int StyleId)
        {
            string check = string.Empty;
            if (Matrix.Any(o => o.ColorId == ColorId && o.StyleId == StyleId)) check = "checked='true'";
            return String.Format("<input type='checkbox' {0} name='itemCheckbox' value='{1}-{2}'/>", check, ColorId, StyleId); ;
        }
        private void LoadData()
        {
            var Colors = VITData.GetQuery<Product_Color>().ToArray();
            var Styles = VITData.GetQuery<Product_Style>().ToArray();
            Matrix = VITData.GetQuery<Product_Color_Style>().ToArray();

            int per = 100 / (Colors.Length + 1);

            StringBuilder str=new StringBuilder();
            str.Append("<table width='100%' align='center'>");
            for (int i = -1; i < Styles.Length; i++)
            {
                if (i == -1)
                {
                    str.Append("<thead>");
                    str.Append("<tr>");
                }
                else str.Append("<tr class='row" + i % 2 + "'>");

                for (int j = -1; j < Colors.Length; j++)
                {
                    if (i == -1)
                    {
                        if (j > -1)
                        {
                            str.AppendFormat("<th width='{0}%'>", per);
                            str.Append(Colors[j].Name);
                            str.Append("</th>");
                        }
                        else
                        {
                            str.Append("<th style='background-color: #ccc;border-right: 1px solid #0074B3;color: #FFFFFF;'>Color/Style</th>");
                        }
                    }
                    else // không phải hàng đầu tiên
                    {
                        if (j > -1)
                        {
                            str.Append("<td align='center'>");
                            str.Append(check(Colors[j].Id, Styles[i].Id));
                            str.Append("</td>");  
                        }
                        else // cột đầu tiên
                        {
                            str.Append("<td align='center' style='background-color: #0085CC;border-right: 1px solid #0074B3;color: #FFFFFF;'>");
                            str.Append(Styles[i].Name);
                            str.Append("</td>");
                        }
                    }  
                }
                if (i == -1) str.Append("</tr></thead><tbody>");
                else str.Append("</tr>");
             }
            str.Append("</tbody></table>");
             divPrint.InnerHtml = str.ToString();
        }
        #endregion

        #region Events
        protected void Apply_Click(object sender, EventArgs e)
        {
            Matrix = VITData.GetQuery<Product_Color_Style>().ToArray();
            string[] check = Request["itemCheckbox"].Split(',');
            VITData.OpenTransaction();
            foreach (var obj in Matrix)
            {
                if (!check.Any(o => o == string.Format("{0}-{1}", obj.ColorId, obj.StyleId)))
                    VITData.DeleteObject<Product_Color_Style>(o => o.ColorId == obj.ColorId && o.StyleId == obj.StyleId);
            }
            foreach (var item in check)
            {
                var obj = new Product_Color_Style();
                string[] mx = item.Split('-');
                
                obj.ColorId = Convert.ToInt32(mx[0]);
                obj.StyleId = Convert.ToInt32(mx[1]);
                obj.CreateByUser = HttpContext.Current.Session["userName"].ToString();
                if (!Matrix.Any(o => o.ColorId == obj.ColorId && o.StyleId == obj.StyleId))
                    VITData.AddObject(obj);
            }
            if (VITData.CommitTransaction())
            {
                lblThanhCong.Visible = true;
                lblThatBai.Visible = false;
            }
            else
            {
                lblThanhCong.Visible = false;
                lblThatBai.Visible = true;
            }
            LoadData();
        }
        #endregion
    }
}
